﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace 不规则三角网体积计算
{
    public partial class Draw : Form
    {
        public Draw()
        {
            InitializeComponent();
        }

        public List<Triangle> T1;
        public string title;
        DXF dxf = new DXF();
        public Draw(List<Triangle> T1,string title)
        {
            this.T1 = T1;
            this.title = title;
            InitializeComponent();
            GDI gdi = new GDI(pictureBox1);
            gdi.drawtri(T1, title);
            dxf.drawtri(T1, "Triangle");
            pictureBox1.Image = gdi.export();

        }
        private void Draw_Load(object sender, EventArgs e)
        {
            pictureBox1.MouseWheel += new MouseEventHandler(pictureBox1_Mousewheel);
            
        }

        private void 保存DXFToolStripMenuItem_Click(object sender, EventArgs e)
        {
            dxf.savefile();
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
           
           pictureBox1.Focus();
        
        }

        private void pictureBox1_Mousewheel(object sender, MouseEventArgs e)
        {
            pictureBox1.Width += e.Delta;
            pictureBox1.Height += e.Delta;
        }

        private void 图像复位ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pictureBox1.Width = this.Width;
            pictureBox1.Height = this.Height;
        }

        private void 保存BMPToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFileDialog1.ShowDialog();
        }

        private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            pictureBox1.Image.Save(saveFileDialog1.FileName);
        }
    }


    class GDI
    {
        public Bitmap bt;
        public Graphics g;
        public Font song1 = new Font("宋体", 10);
        public Font song2 = new Font("宋体", 20);

        public GDI()
        {

        }

        public GDI(PictureBox pic)
        {
            bt = new Bitmap(pic.Width, pic.Height);
            g = Graphics.FromImage(bt);
            g.Clear(Color.SkyBlue);
            g.TranslateTransform(50, bt.Height - 50);
        }

        public void drawtri(List<Triangle> tri, string title)
        {
            Pen redpen = new Pen(Color.Red);
            Pen blackpen = new Pen(Color.Black, 1);

            SolidBrush bru = new SolidBrush(Color.Red);

            int tricount = tri.Count;
            List<double> x = new List<double>();
            List<double> y = new List<double>();

            for (int i = 0; i < tricount; i++)
            {
                x.Add(tri[i].p1.x);
                x.Add(tri[i].p2.x);
                x.Add(tri[i].p3.x);
                y.Add(tri[i].p1.y);
                y.Add(tri[i].p2.y);
                y.Add(tri[i].p3.y);
            }

            double xmax = x.Max();
            double xmin = x.Min();
            double ymax = y.Max();
            double ymin = y.Min();

            //坐标比例关系
            double xscale = (bt.Width - 100) / (xmax - xmin);
            double yscale = (bt.Height - 100) / (ymax - ymin);

            //坐标轴间隔距离
            double xdis = (bt.Width - 100) / 9.0;
            double ydis = (bt.Height - 100) / 9.0;

            //坐标标注
            double xlab = (xmax - xmin) / 9.0;
            double ylab = (ymax - ymin) / 9.0;

            //绘制三角网
            for (int i = 0; i < tricount; i++)
            {
                PointF[] p = new PointF[4];
                p[0] = new PointF((float)((tri[i].p1.x - xmin) * xscale), -(float)((tri[i].p1.y - ymin) * yscale));
                p[1] = new PointF((float)((tri[i].p2.x - xmin) * xscale), -(float)((tri[i].p2.y - ymin) * yscale));
                p[2] = new PointF((float)((tri[i].p3.x - xmin) * xscale), -(float)((tri[i].p3.y - ymin) * yscale));
                p[3] = new PointF((float)((tri[i].p1.x - xmin) * xscale), -(float)((tri[i].p1.y - ymin) * yscale));
                g.DrawLines(blackpen, p);
            }

            //绘制标题
            g.DrawString(title, song2, bru, (float)(bt.Width * 0.4), -(float)(bt.Height * 0.9));
        }

        public Bitmap export()
        {
            return bt;
        }
    }

    class DXF
    {
        public string begin = 0 + "\r\n" + "SECTION\r\n" + 2 + "\r\n" + "ENTITIES" + "\r\n";
        public string end = 0 + "\r\n" + "ENDSEC\r\n" + 0 + "\r\n" + "EOF" + "\r\n";
        public string str = null;

        public DXF()
        {

        }

        /// <summary>
        /// 绘制三角网
        /// </summary>
        /// <param name="tri">三角形数组</param>
        /// <param name="title">标题</param>
        public void drawtri(List<Triangle> tri, string title)
        {
            for (int i = 0; i < tri.Count; i++)
            {
                str += 0 + "\r\n" + "LINE\r\n" + 8 + "\r\n" + 0 + "\r\n" + 10 + "\r\n" + tri[i].p1.x + "\r\n" + 20 + "\r\n" + tri[i].p1.y + "\r\n" + 30 + "\r\n" + 0 + "\r\n"
                  + 11 + "\r\n" + tri[i].p2.x + "\r\n" + 21 + "\r\n" + tri[i].p2.y + "\r\n" + 31 + "\r\n" + 0 + "\r\n";
                str += 0 + "\r\n" + "LINE\r\n" + 8 + "\r\n" + 0 + "\r\n" + 10 + "\r\n" + tri[i].p1.x + "\r\n" + 20 + "\r\n" + tri[i].p1.y + "\r\n" + 30 + "\r\n" + 0 + "\r\n"
                  + 11 + "\r\n" + tri[i].p3.x + "\r\n" + 21 + "\r\n" + tri[i].p3.y + "\r\n" + 31 + "\r\n" + 0 + "\r\n";
                str += 0 + "\r\n" + "LINE\r\n" + 8 + "\r\n" + 0 + "\r\n" + 10 + "\r\n" + tri[i].p3.x + "\r\n" + 20 + "\r\n" + tri[i].p3.y + "\r\n" + 30 + "\r\n" + 0 + "\r\n"
                  + 11 + "\r\n" + tri[i].p2.x + "\r\n" + 21 + "\r\n" + tri[i].p2.y + "\r\n" + 31 + "\r\n" + 0 + "\r\n";
            }
        }

        /// <summary>
        /// 保存DXF文件函数
        /// </summary>
        public void savefile()
        {
            SaveFileDialog save = new SaveFileDialog();
            save.Filter = "dxf文件.dxf|*.dxf";

            if (save.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    using (StreamWriter sw = new StreamWriter(save.FileName))
                    {
                        sw.Write(begin + str + end);
                    }

                    System.Diagnostics.Process open = new System.Diagnostics.Process();
                    open.StartInfo.FileName = save.FileName;
                    open.Start();
                }
                catch (Exception q)
                {
                    MessageBox.Show(q.Message);
                }
            }

        }

    }


}
